package com.portal.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.portal.mapper.AddressMapper;
import com.portal.model.entity.Address;
import com.portal.model.vo.AddressVo;
import com.portal.service.AddressService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public  class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements AddressService {
    @Override
    public List<AddressVo> findAddressByUserId(Long userId) {
        // SELECT * FROM `t_address` where user_id=1
        // SELECT id,provice,city.. FROM `t_address` where user_id=1
        Wrapper<Address> wrapper =
                Wrappers.lambdaQuery(Address.class)
                        .select(Address::getId, Address::getProvince, Address::getCity,
                                Address::getArea,Address::getDetailAddress, Address::getIsDefault)
                        .eq(Address::getUserId, userId);
        List<Address> list = this.list(wrapper);
        return list.stream().map(item ->
                new AddressVo(
                        item.getId(),
                        item.getProvince() + item.getCity() + item.getArea() + item.getDetailAddress(),
                        item.getIsDefault())
        ).collect(Collectors.toList());
    }
}
